import Swinject

final class DependencyContainer {
    static let shared = DependencyContainer()
    
    private let container: Container
    
    private init() {
        container = Container()
        setupDependencies()
    }
    
    private func setupDependencies() {
        // 注册Repository协议绑定
        container.register(RepositoryProtocl.self) { _ in
            MockComponentsRepositoryImp()
        }.inObjectScope(.container)
        
        container.register(UserRepository.self) { _ in
            MockUserRepository()
        }.inObjectScope(.container)
        
        // 注册UseCase绑定
        container.register(ComponentsUseCase.self) { resolver in
            ComponentsUseCaseImp()
        }
        
        container.register(LoginUseCase.self) { resolver in
            LoginUseCaseImp()
        }
    }
    
    func resolve<T>(_ serviceType: T.Type) -> T {
        guard let value = container.resolve(serviceType) else {
        fatalError("未找到\(serviceType)的注册实现，请检查依赖注入配置")
    }
    return value
    }
}

@propertyWrapper
struct Inject<T> {
    let wrappedValue: T
    
    init() {
        self.wrappedValue = DependencyContainer.shared.resolve(T.self)
    }
}
